package com.alatus;

import com.alatus.utils.GameUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWin extends JFrame {
    private int width = 2 * GameUtil.OFFSET + GameUtil.MAP_WIDTH * GameUtil.SQUARE_LENGTH;
    private int height = 4 * GameUtil.OFFSET + GameUtil.MAP_HEIGHT * GameUtil.SQUARE_LENGTH;
    private MapBottom mapBottom = new MapBottom();
    private MapTop mapTop = new MapTop();
//    画布,使用双缓存设计解决闪动问题
    private Image offScreenImage = null;
//    是否开始
    private boolean begin = false;
    private GameSelect gameSelect = new GameSelect();
    private void launch(){
        GameUtil.START_TIME = System.currentTimeMillis();
        this.setVisible(true);
        if(GameUtil.state == 3){
            this.setSize(500,500);
        }
        else{
            this.setSize(width,height);
        }
//        设置窗口位置,居中显示
        this.setLocationRelativeTo(null);
//        设置标题
        this.setTitle("扫雷");
//        设置关闭方法
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//        鼠标事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                switch (GameUtil.state){
                    case 0:
//                鼠标左键
                        if(e.getButton() == 1){
                            GameUtil.MOUSE_X = e.getX();
                            GameUtil.MOUSE_Y = e.getY();
                            GameUtil.LEFT = true;
                        }
//                鼠标右键
                        if(e.getButton() == 3){
                            GameUtil.MOUSE_X = e.getX();
                            GameUtil.MOUSE_Y = e.getY();
                            GameUtil.RIGHT = true;
                        }
                    case 1:
                        mapTop.paintSelf(getGraphics());
                        mapBottom.paintSelf(getGraphics());
                    case 2:
                        if(e.getButton() == 1){
                            if(e.getX() > GameUtil.OFFSET + GameUtil.SQUARE_LENGTH*(GameUtil.MAP_WIDTH/2)&&
                            e.getX()<GameUtil.OFFSET + GameUtil.SQUARE_LENGTH*(GameUtil.MAP_WIDTH/2)+GameUtil.SQUARE_LENGTH&&
                            e.getY()>GameUtil.OFFSET&&
                            e.getY()<GameUtil.OFFSET+GameUtil.SQUARE_LENGTH){
                                mapBottom.reGame();
                                mapTop.reGame();
                                GameUtil.FLAG_NUM = 0;
                                GameUtil.state = 0;
                                GameUtil.START_TIME = System.currentTimeMillis();
                            }
                        }
                        if(e.getButton() == 2){
                            GameUtil.state = 3;
                            begin = true;
                        }
                        break;
                    case 3:
                        if(e.getButton() == 1){
                            GameUtil.MOUSE_X = e.getX();
                            GameUtil.MOUSE_Y = e.getY();
                            begin = gameSelect.hard();
                        }
                        break;
                    default:
                }
            }
        });
        while(true){
            repaint();
            begin();
        }
    }

    void begin(){
        if(begin){
            begin = false;
            gameSelect.hard(GameUtil.level);
            dispose();
            GameUtil.FLAG_NUM = 0;
            GameWin gameWin = new GameWin();
            GameUtil.START_TIME = System.currentTimeMillis();
            mapBottom.reGame();
            mapTop.reGame();
            gameWin.launch();
        }
    }

    public static void main(String[] args) {
        new GameWin().launch();
    }

    @Override
    public void paint(Graphics graphics) {
        if(GameUtil.state == 3){
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0,0,500,500);
            gameSelect.paintSelf(graphics);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        else{
            offScreenImage = this.createImage(width,height);
            Graphics gImage = offScreenImage.getGraphics();
//        设置背景颜色
            gImage.setColor(Color.ORANGE);
            gImage.fillRect(0,0,width,height);
            mapBottom.paintSelf(gImage);
            mapTop.paintSelf(gImage);
            graphics.drawImage(offScreenImage,0,0,null);
        }
    }
}
